package vcp.gui.forms;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import client.Message;
import db.parking.beans.Orders;

import vcp.common.IView;
import vcp.gui.components.VCPPanel;
import javax.swing.border.TitledBorder;

/**
 * Track order form
 *
 */
public class OrderTrackForm extends VCPPanel implements Observer,IView{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textOrderID;
	private JLabel lblOrderID;
	private JButton btnCheck;
	private GroupLayout groupLayout;
	private JLabel lblCurrentStatus;
	private JLabel lbl_status;

	/**
	 * Create the panel.
	 */
	public OrderTrackForm() {
		setBorder(new TitledBorder(null, "Track order", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(Color.white);
		lblOrderID = new JLabel("Order ID");
		
		textOrderID = new JTextField();
		textOrderID.setColumns(10);
		
		btnCheck = new JButton("check");
		lblCurrentStatus = new JLabel("Order Status");
		lblCurrentStatus.setVisible(false);
		lbl_status = new JLabel("");
		lbl_status.setVisible(false);
		groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblCurrentStatus, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lbl_status))
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
							.addComponent(btnCheck)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblOrderID)
								.addGap(18)
								.addComponent(textOrderID, GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))))
					.addGap(23))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblOrderID)
						.addComponent(textOrderID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnCheck)
					.addGap(10)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCurrentStatus)
						.addComponent(lbl_status))
					.addContainerGap(207, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}

	@Override
	public void addController(Object obj) {
		btnCheck.addActionListener((ActionListener)obj);
		btnCheck.setActionCommand("orderTrack");
	}

	@Override
	public void update(Observable arg0, Object arg) {
		Message tempMsg = (Message)arg;  
		Orders order = (Orders) tempMsg.getObj();

		switch(tempMsg.getMsg())
		 {
		 	case "TrackOrder":
				if(tempMsg.isStatus())
				{
					String s = null;
					switch(order.getStatus())
					{
					case 0:
						s = "Awaiting User Arrical";
						break;
					case 1:
						s = "Car Is Parked Inside Lot";
						break;
					case 2:
						s = "User Has Finihsed Parking";
						break;
					case 3:
						s = "Parking Has Been Canceled";
						break;
					case 4:
						s = "Fine Received - User Is Late To Parking";
						break;	
					case 5:
						s = "Fine Received - User Is Late To Check Out From Lot";
						break;	
						
					}
					lblCurrentStatus.setVisible(true);
					lbl_status.setVisible(true);
					lbl_status.setText(s);
					
				}	
				else
				{
					JOptionPane.showMessageDialog(null," Track order has Failed: " + tempMsg.getStatusMsg(),"DATABASE ERROR",JOptionPane.ERROR_MESSAGE);
				}
				break;
		 }
	}
			  

	public JTextField getTextOrderID() {
		return textOrderID;
	}

	public void setTextOrderID(JTextField textOrderID) {
		this.textOrderID = textOrderID;
	}

	public JLabel getLblOrderID() {
		return lblOrderID;
	}

	public void setLblOrderID(JLabel lblOrderID) {
		this.lblOrderID = lblOrderID;
	}

	public JButton getBtnCheck() {
		return btnCheck;
	}

	public void setBtnCheck(JButton btnCheck) {
		this.btnCheck = btnCheck;
	}

	public GroupLayout getGroupLayout() {
		return groupLayout;
	}

	public void setGroupLayout(GroupLayout groupLayout) {
		this.groupLayout = groupLayout;
	}
}
